package com.example.admin.goodnews.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.utils.CacheManager;

import java.io.File;
import java.math.BigDecimal;
import java.math.MathContext;

/**
 * 系统设置页面
 * Created by admin on 2016/9/29.
 */

public class SetupActivity extends AppCompatActivity implements View.OnClickListener {
    private RelativeLayout relativelayout_setup;
    private RelativeLayout arrow_back;// 标题栏返回按钮
    private LinearLayout changefontsize;// 字体调节
    private RelativeLayout cleancache;// 清除缓存
    private RelativeLayout about;// 关于
    private RelativeLayout privacypolicy;// 隐私政策
    private RelativeLayout checkupdate;// 检查更新
    private RelativeLayout activity;// 活动
    private Button clicktologin;// 点击登录
    private Button clicktologout;// 点击退出
    private TextView tv_cachecount;// 缓存的大小

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        relativelayout_setup = (RelativeLayout) findViewById(R.id.relativelayout_setup);
        arrow_back = (RelativeLayout) findViewById(R.id.arrow_back);// 标题栏返回按钮
        changefontsize = (LinearLayout) findViewById(R.id.changefontsize);// 字体调节
        cleancache = (RelativeLayout) findViewById(R.id.cleancache);// 清除缓存
        about = (RelativeLayout) findViewById(R.id.about);// 关于
        privacypolicy = (RelativeLayout) findViewById(R.id.privacypolicy);// 隐私政策
        checkupdate = (RelativeLayout) findViewById(R.id.checkupdate);// 检查更新
        activity = (RelativeLayout) findViewById(R.id.activity);// 活动
        clicktologin = (Button) findViewById(R.id.clicktologin);// 点击登录
        clicktologout = (Button) findViewById(R.id.clicktologout);// 点击退出
        tv_cachecount = (TextView) findViewById(R.id.tv_cachecount);// 缓存的大小
        tv_cachecount.setText(getCache());
    }

    private void initData() {

    }

    private void initEvent() {
        arrow_back.setOnClickListener(SetupActivity.this);// 标题栏返回按钮
        changefontsize.setOnClickListener(SetupActivity.this);// 字体调节
        cleancache.setOnClickListener(SetupActivity.this);// 清除缓存
        about.setOnClickListener(SetupActivity.this);// 关于
        privacypolicy.setOnClickListener(SetupActivity.this);// 隐私政策
        checkupdate.setOnClickListener(SetupActivity.this);// 检查更新
        activity.setOnClickListener(SetupActivity.this);// 活动
        clicktologin.setOnClickListener(SetupActivity.this);// 点击登录
        clicktologout.setOnClickListener(SetupActivity.this);// 点击退出
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // 标题栏返回按钮
            case R.id.arrow_back:
                finish();
                break;
            // 字体调节
            case R.id.changefontsize:
                break;
            // 清除缓存
            case R.id.cleancache:
                CacheManager.clearAllCache(SetupActivity.this);
                tv_cachecount.setText("0 K");
                Snackbar.make(relativelayout_setup, "缓存清理完成", Snackbar.LENGTH_LONG).show();
                break;
            // 关于
            case R.id.about:
                break;
            // 隐私政策
            case R.id.privacypolicy:
                String privacy = "file:///android_asset/privacy.html";
                Intent intent = new Intent(SetupActivity.this, SecondaryPageActivity.class);
                intent.putExtra("link", privacy);
                intent.putExtra("title", "隐私政策");
                startActivity(intent);
                break;
            // 检查更新
            case R.id.checkupdate:
                break;
            // 活动
            case R.id.activity:
                break;
            // 点击登录
            case R.id.clicktologin:
                Intent intent_clicktologin = new Intent(SetupActivity.this, LoginActivity.class);
                startActivity(intent_clicktologin);
                break;
            // 点击退出
            case R.id.clicktologout:
                break;
        }
    }

    // 获取缓存大小
    private String getCache() {
        String filesize = "";
        try {
            filesize = CacheManager.getTotalCacheSize(SetupActivity.this);
            Log.e("cache", filesize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return filesize;
    }

}
